"""
  Copyright (c) 2024 Northeastern University (China)
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
"""
import json
import bson
import io
from . import mongodb
from ..common_utils.utils_file import decompress_dict


class InitialSig(mongodb.MongoDB):
    db_name = "HarmonyOSS"
    collection_name ="InitialSig"
    def __init__(self):
        super().__init__("HarmonyOSS", "InitialSig")

    def get_data(self,filter,field,count=0):
        return self.get(filter,field,count)

    def get_data_count(self):
        return self.get_document_count()

    def get_many_data(self, filter, field):
        large_data = self.get_large_data(filter, field)
        for data in large_data:
            yield data
    def add_meta(self, dic):
        self.add_one(dic)
    def update_one(self,_id,content):
        #利用id修改值
        return self.update_one_with_id(_id,content)
    def add_meta_update_repeat(self,dic):
        res=self.get_data({'sig_full_nme':dic['sig_full_name']},{})
        if len(res) == 0:
            self.add_one(dic)
        else:
            self.update_one(res[0]['_id'], dic)
    def Gridfs_store(self,file,filename):
        return self.use_Gridfs_store(file,filename)
    def Gridfs_get(self,file_id):
        return self.use_Gridfs_get(file_id)
    def Gridfs_remove(self,file_id):
        return self.use_Gridfs_delete(file_id)

    def get_all_sigs(self, filter, field, count: int = 0):
        all = self.get_large_data(filter, field, count, db_name=self.db_name, collection_name=self.collection_name)
        for doc in all:
            sig_info = doc['sig_info']
            if isinstance(sig_info, bytes):
                data = decompress_dict(sig_info)
                doc['sig_info'] = data
            elif isinstance(sig_info, bson.objectid.ObjectId):
                real_info = self.Gridfs_get(sig_info)
                if real_info is not None:
                    data = decompress_dict(real_info)
                    doc['sig_info'] = data
            else:
                print(f"sig_info类型错误")
            yield doc

    def get_one_sig(self, filter, field, count: int = 0):
        all = self.get(filter, field, count)
        for doc in all:
            sig_info = doc['sig_info']
            if isinstance(sig_info, bytes):
                data = decompress_dict(sig_info)
                doc['sig_info'] = data
            elif isinstance(sig_info, bson.objectid.ObjectId):
                real_info = self.Gridfs_get(sig_info)
                if real_info is not None:
                    data = decompress_dict(real_info)
                    doc['sig_info'] = data
            else:
                print(f"sig_info类型错误")
        return all
